package com.tencent.externalstoragebar.service;

import com.tencent.externalstoragebar.view.StorageBarView;
import com.tencent.externalstoragebar.evetntools.OneEvent;
import com.tencent.externalstoragebar.evetntools.TouchListenSever;
import com.tencent.externalstoragebar.tools.l;
import com.tencent.externalstoragebar.publicthing.PublicData;

//蓄力服务
public class StorageService extends TouchListenSever {
    //是否结束
    public static boolean over=false;

    //生效手指的id
    public static int nowFingerId=-1;

    @Override
    public void onTouchEventOnce(OneEvent event) {
        //这里拿到触摸事件
        //先打印试试
        l.l("("+event.getX()+","+event.getY()+")");

        //如果悬浮窗没有启动,直接返回
        if (!FloatingWindowService.isStart){
            return;
        }
        //尝试播放蓄力条动画
        if (needAction(event)){
            FloatingWindowService.handler.post(new Runnable() {
                @Override
                public void run() {
                    //这里执行播放逻辑
                    ((StorageBarView)FloatingWindowService.floatingView).startAnimation();
                }
            });
        }

        //如果松手,则取消动画
        if (needStop(event)){
            FloatingWindowService.handler.post(new Runnable() {
                @Override
                public void run() {
                    //这里执行取消动画行为
                    l.l("取消动画");

                    ((StorageBarView)FloatingWindowService.floatingView).stopAnimation();

                }
            });
        }

    }

    //是否需要停止方法
    public static boolean needStop(OneEvent event){
        //停止条件:
        //手指id为nowFingerId
        //并且状态为抬起

        if (event.getIndex()==nowFingerId&&event.getEvent()==OneEvent.IS_UP){
            //销毁手指id
            nowFingerId=-1;
            return true;
        }
        return false;
    }

    //是否需要启动方法
    public static boolean needAction(OneEvent event){
        //启动条件:
        //触摸按下坐标在(x,y)处半径为r的圆内
        //如果状态不为按下直接返回
        if (event.getEvent()!=OneEvent.IS_DOWN){
            return false;
        }

        int x= PublicData.a;
        int y= PublicData.b;
        int r= PublicData.c;

        //开始判断
        if ((event.getX()-x)*(event.getX()-x)+(event.getY()-y)*(event.getY()-y)<=r*r){
            //记录当前手指的id
            nowFingerId=event.getIndex();
            return true;
        }
        return false;
    }

    //结束方法
    @Override
    public boolean over() {
        return over;
    }
}